#version 330 core

// Input Variables
in vec3 view_Half;
in vec3 view_Normal;
in vec3 view_LightDir;
in vec3 world_Pos;

// Uinform Variables
struct PointLight
{
vec3 pos;
vec3 color;
};
uniform PointLight light;

struct PointMaterial
{
vec3 kd;
vec3 ks;
float shininess;
};
uniform PointMaterial material;

// Output Variables
layout(location = 0) out vec4 out_color;

float computeRadialAttenuation(float d)
{
float a0, a1, a2, A1;
a0 = 1.0;
a1 = 0.5;
a2 = 0.5;
A1 = 1/(a0 + a1*d +a2*d*d); 
return A1;
}

void main()
{
vec3 N, H, L, color;
float lightDist, Ar, diffuse, specular;

N = normalize(view_Normal);
H = normalize(view_Half);
L = normalize(view_LightDir);
diffuse = max(0, dot(N, L));
specular = pow(max(0, dot(H, N)), material.shininess);
lightDist = length(light.pos - world_Pos);
Ar = computeRadialAttenuation(lightDist);
color = light.color*diffuse*Ar*(material.kd + material.ks*specular);
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
out_color = vec4(color, 1.0);
}